package com.gwtcx.client.smartgwt.view;

import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtcx.client.smartgwt.view.handlers.ErrorPageUiHandlers;
import com.gwtcx.client.util.I18nUtil;
import com.gwtplatform.mvp.client.UiHandlers;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

public abstract class ErrorPageView<C extends UiHandlers> extends ViewWithUiHandlers<C> {

	  public static final String OK_BUTTON_CONTAINER = "okButtonContainer";

	  private final String html = "<div>\n"
	    + "<table align=\"center\">\n"
	    + "  <tr>\n" + "<td>&nbsp;</td>\n" + "</tr>\n"
	    + "  <tr>\n" + "<td>&nbsp;</td>\n" + "</tr>\n"
	    + "  <tr>\n" + "<td>&nbsp;</td>\n" + "</tr>\n"
	    + "  <tr>\n"
	    + "    <td style=\"font-weight:bold;\">An error has occurred.</td>\n"
	    + "  </tr>\n"
	    + "  <tr>\n" + "<td>&nbsp;</td>\n" + "</tr>\n"
	    + "  <tr>\n" + "<td>Try this action again. If the problem continues,</td>\n" + "</tr>\n"
	    + "  <tr>\n" + "<td>check the application's support forums for a solution or</td>\n" + "</tr>\n"
	    + "  <tr>\n" + "<td>contact the application's administrator.</td>\n" + "</tr>\n"
	    + "  <tr>\n" + "<td>&nbsp;</td>\n" + "</tr>\n"
	    + "  <tr>\n"
	    + "    <td id=\"okButtonContainer\"></td>\n"
	    + "  </tr>\n"
	    + "</table>\n"
	    + "</div>\n";

	  private final HTMLPanel panel;
	  private final Button okButton;

	  public ErrorPageView() {

	    panel = new HTMLPanel(getPanelHtml());

	    okButton = new Button(I18nUtil.getConstant().ok());

	    panel.add(okButton, OK_BUTTON_CONTAINER);

	    bindCustomUiHandlers();
	  }

	  protected void bindCustomUiHandlers() {

	      getOkButton().addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	        if (getUiHandlers() != null) {
	          ((ErrorPageUiHandlers) getUiHandlers()).onOkButtonClicked();
	        }
	      }
	    });
	  }

	  @Override
	  public Widget asWidget() {
	    return panel;
	  }

	  protected String getPanelHtml() {
	    return html;
	  }

	  public Button getOkButton() {
	    return okButton;
	  }
	}
